// Do file that puts together the YPSPS data to be used for the paper "Persistent Inequalities"
//Generations 1 and 2

clear all

use "E:\ProjData\AdoptionStudy\YPSPS\04532-0001-Data.dta", clear



*** Gen indicator for unique grandparent-parent pair
bysort v5002 g320: gen uniquegen1gen2 = _n if g320!=9999
rename v5002 famid


*** Gen dummies for turnout 1996
gen turnoutparent96 = v5800
replace turnoutparent96 = 0 if v5800==5


*** Gen dummies for turnout 1992
gen turnoutparent92 = v5809
replace turnoutparent92 = 0 if v5809==5


*** Gen dummies for turnout 1988
gen turnoutparent88 = v5813
replace turnoutparent88 = 0 if v5813==5


*** Gen dummies for turnout 1984
gen turnoutparent84 = v5815
replace turnoutparent84 = 0 if v5815==5


*** Gen dummies for turnout 1980
gen turnoutparent80 = .
replace turnoutparent80 = 0 if v1621==5
replace turnoutparent80 = 1 if v1621==1

gen turnoutgrandmother80 = .
replace turnoutgrandmother80 = 0 if v1803==5
replace turnoutgrandmother80 = 1 if v1803==1

gen turnoutgrandfather80 = .
replace turnoutgrandfather80 = 0 if v1808==5
replace turnoutgrandfather80 = 1 if v1808==1

egen turnoutgrandparent80 = rowmean(turnoutgrandmother80 turnoutgrandfather80)


*** Gen dummies for turnout 1976
gen turnoutparent76 = .
replace turnoutparent76 = 0 if v1630==5
replace turnoutparent76 = 1 if v1630==1


*** Gen dummies for turnout 1972
gen turnoutparent72 = .
replace turnoutparent72 = 0 if v502==5
replace turnoutparent72 = 1 if v502==1

gen turnoutgrandmother72 = .
replace turnoutgrandmother72 = 0 if v586==5
replace turnoutgrandmother72 = 1 if v586==1

gen turnoutgrandfather72 = .
replace turnoutgrandfather72 = 0 if v592==5
replace turnoutgrandfather72 = 1 if v592==1

egen turnoutgrandparent72 = rowmean(turnoutgrandmother72 turnoutgrandfather72)


*** Gen dummies for turnout 1968
gen turnoutparent68 = .
replace turnoutparent68 = 0 if v512==5
replace turnoutparent68 = 1 if v512==1


*** Gen dummies for turnout 1964
gen turnoutgrandparent64 = .
replace turnoutgrandparent64 = 0 if inrange(g387, 5, 8)
replace turnoutgrandparent64 = 1 if inrange(g387, 1, 4)


*** Sex, birth year and race
gen kidrace = v6601
gen kidfemale = v6600-1
gen parentfemale = g583-1
gen kidbyear = v5030 + 1900
gen parentbyear = g546+1900 if inrange(g546, 0, 39)
replace parentbyear = g546+1800 if inrange(g546, 90, 99)


***Generate some additional variables***
*Indicator for having parental turnout info from all 3 elections
foreach num in 64 72 80 {
gen occturnoutgrandparent`num' = 1 if `name'`num'!=.

}
egen occturnoutgrandparentRost = rowtotal(occturnoutgrandparent64 occturnoutgrandparent72 occturnoutgrandparent80)
tab occturnoutgrandparentRost
replace occturnoutgrandparentRost = . if inrange(occturnoutgrandparentRost, 0, 2)

gen parent_all = occturnoutgrandparentRost
replace parent_all = 1 if parent_all!=.
drop occ*
keep if parent_all==1


*2 indicators for stability in parental voting
*Variation in stability is due both to over time variation within parent
*and across parent variation at a specific time point
*2nd indicator: Herfindahl's index standardized to 0-1 range
egen parentconsistent = rowmean(turnoutgrandparent64 turnoutgrandparent72 turnoutgrandparent80)
replace parentconsistent = 1 if parentconsistent==0
replace parentconsistent = 0 if inrange(parentconsistent, 0.05, 0.95)
egen occ1 = rowmean(turnoutgrandparent64 turnoutgrandparent72 turnoutgrandparent80)
gen parentconsistentHI = -1*(1-(occ1^2+(1-occ1)^2))/0.5+1



*** 1968 sample
preserve
keep turnoutparent68 uniquegen1gen2 parentconsistent parentconsistentHI parentbyear kidrace kidfemale kidbyear parentfemale famid turnoutgrandparent64
keep if uniquegen1gen2==1
gen electionyear = 1968
gen kidelectionage = 1968-kidbyear-18
drop if kidelectionage<0
rename turnoutparent68 turnoutkid
egen turnoutparentcum = rowmean(turnoutgrandparent64)
tempfile parent1968sample
save `parent1968sample'
restore


*** 1972 sample
preserve
keep turnoutparent72 uniquegen1gen2 parentconsistent parentconsistentHI parentbyear kidrace kidfemale kidbyear parentfemale famid turnoutgrandparent64
keep if uniquegen1gen2==1
gen electionyear = 1972
gen kidelectionage = 1972-kidbyear-18
drop if kidelectionage<0
rename turnoutparent72 turnoutkid
egen turnoutparentcum = rowmean(turnoutgrandparent64)
tempfile parent1972sample
save `parent1972sample'
restore


*** 1976 sample
preserve
keep turnoutparent76 uniquegen1gen2 parentconsistent parentconsistentHI parentbyear kidrace kidfemale kidbyear parentfemale famid turnoutgrandparent64 turnoutgrandparent72
keep if uniquegen1gen2==1
gen electionyear = 1976
gen kidelectionage = 1976-kidbyear-18
drop if kidelectionage<0
rename turnoutparent76 turnoutkid
egen turnoutparentcum = rowmean(turnoutgrandparent64 turnoutgrandparent72)
tempfile parent1976sample
save `parent1976sample'
restore


*** 1980 sample
preserve
keep turnoutparent80 uniquegen1gen2 parentconsistent parentconsistentHI parentbyear kidrace kidfemale kidbyear parentfemale famid turnoutgrandparent64 turnoutgrandparent72
keep if uniquegen1gen2==1
gen electionyear = 1980
gen kidelectionage = 1980-kidbyear-18
drop if kidelectionage<0
rename turnoutparent80 turnoutkid
egen turnoutparentcum = rowmean(turnoutgrandparent64 turnoutgrandparent72)
tempfile parent1980sample
save `parent1980sample'
restore


*** 1984 sample
preserve
keep turnoutparent84 uniquegen1gen2 parentconsistent parentconsistentHI parentbyear kidrace kidfemale kidbyear parentfemale famid turnoutgrandparent64 turnoutgrandparent72 turnoutgrandparent80
keep if uniquegen1gen2==1
gen electionyear = 1984
gen kidelectionage = 1984-kidbyear-18
drop if kidelectionage<0
rename turnoutparent84 turnoutkid
egen turnoutparentcum = rowmean(turnoutgrandparent64 turnoutgrandparent72 turnoutgrandparent80)
tempfile parent1984sample
save `parent1984sample'
restore


*** 1988 sample
preserve
keep turnoutparent88 uniquegen1gen2 parentconsistent parentconsistentHI parentbyear kidrace kidfemale kidbyear parentfemale famid turnoutgrandparent64 turnoutgrandparent72 turnoutgrandparent80
keep if uniquegen1gen2==1
gen electionyear = 1988
gen kidelectionage = 1988-kidbyear-18
drop if kidelectionage<0
rename turnoutparent88 turnoutkid
egen turnoutparentcum = rowmean(turnoutgrandparent64 turnoutgrandparent72 turnoutgrandparent80)
tempfile parent1988sample
save `parent1988sample'
restore


*** 1992 sample
preserve
keep turnoutparent92 uniquegen1gen2 parentconsistent parentconsistentHI parentbyear kidrace kidfemale kidbyear parentfemale famid turnoutgrandparent64 turnoutgrandparent72 turnoutgrandparent80
keep if uniquegen1gen2==1
gen electionyear = 1992
gen kidelectionage = 1992-kidbyear-18
drop if kidelectionage<0
rename turnoutparent92 turnoutkid
egen turnoutparentcum = rowmean(turnoutgrandparent64 turnoutgrandparent72 turnoutgrandparent80)
tempfile parent1992sample
save `parent1992sample'
restore


*** 1996 sample
preserve
keep turnoutparent96 uniquegen1gen2 parentconsistent parentconsistentHI parentbyear kidrace kidfemale kidbyear parentfemale famid turnoutgrandparent64 turnoutgrandparent72 turnoutgrandparent80
keep if uniquegen1gen2==1
gen electionyear = 1996
gen kidelectionage = 1996-kidbyear-18
drop if kidelectionage<0
rename turnoutparent96 turnoutkid
egen turnoutparentcum = rowmean(turnoutgrandparent64 turnoutgrandparent72 turnoutgrandparent80)
tempfile parent1996sample
save `parent1996sample'
restore


use `parent1968sample', clear
append using `parent1972sample'
append using `parent1976sample'
append using `parent1980sample'
append using `parent1984sample'
append using `parent1988sample'
append using `parent1992sample'
append using `parent1996sample'

drop uniquegen1gen2 turnoutgrandparent64 turnoutgrandparent72 turnoutgrandparent80
drop if turnoutkid==.


*gen lnkidelectionage = ln(kidelectionage+1)



* All parents
reg turnoutkid turnoutparentcum i.parentbyear kidfemale i.kidelectionage i.electionyear if (electionyear==1968 | electionyear==1976 |electionyear==1984), cluster(famid)
reg turnoutkid c.turnoutparentcum##c.parentconsistent i.parentbyear##c.parentconsistent c.kidfemale##c.parentconsistent i.kidelectionage##c.parentconsistent i.electionyear##c.parentconsistent if (electionyear==1968 | electionyear==1976 |electionyear==1984), cluster(famid)
reg turnoutkid c.turnoutparentcum##c.parentconsistentHI i.parentbyear##c.parentconsistentHI c.kidfemale##c.parentconsistentHI i.kidelectionage##c.parentconsistentHI i.electionyear##c.parentconsistentHI if (electionyear==1968 | electionyear==1976 |electionyear==1984), cluster(famid)
reg turnoutkid turnoutparentcum c.turnoutparentcum#c.kidelectionage i.parentbyear i.parentbyear#c.kidelectionage kidfemale c.kidfemale#c.kidelectionage i.kidelectionage i.electionyear i.electionyear#c.kidelectionage if (electionyear==1968 | electionyear==1976 |electionyear==1984), cluster(famid)

reg turnoutkid turnoutparentcum i.parentbyear kidfemale i.kidelectionage i.electionyear, cluster(famid)
reg turnoutkid c.turnoutparentcum##c.parentconsistent i.parentbyear##c.parentconsistent c.kidfemale##c.parentconsistent i.kidelectionage##c.parentconsistent i.electionyear##c.parentconsistent, cluster(famid)
reg turnoutkid c.turnoutparentcum##c.parentconsistentHI i.parentbyear##c.parentconsistentHI c.kidfemale##c.parentconsistentHI i.kidelectionage##c.parentconsistentHI i.electionyear##c.parentconsistentHI, cluster(famid)
reg turnoutkid turnoutparentcum c.turnoutparentcum#c.kidelectionage i.parentbyear i.parentbyear#c.kidelectionage kidfemale c.kidfemale#c.kidelectionage i.kidelectionage i.electionyear i.electionyear#c.kidelectionage, cluster(famid)




*Save for pooled sample
keep if (electionyear==1968 | electionyear==1976 | electionyear==1984)
keep famid turnoutkid kidfemale parentbyear parentconsistent parentconsistentHI electionyear kidelectionage turnoutparentcum
gen sample = 1
gen kidelectionage01 = kidelectionage/32

save "E:\ProjData\AdoptionStudy\YPSPS\YPSPS1.dta", replace

